/*
 * Copyright 2010, Andrew M Gibson
 *
 * www.andygibson.net
 *
 * This file is part of DataValve.
 *
 * DataValve is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DataValve is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DataValve.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fluttercode.datavalve.provider.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.RowProcessor;

/**
 * 把ResultSet当前行转换为一个Object[]
 */
public class ArrayMapper implements ResultSetObjectMapper<Object[]> {

    /**
     * Singleton processor instance that handlers share to save memory.
     * Notice the default scoping to allow only classes in this package to use
     * this instance.
     */
    static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor();

    /**
     * Convert a <code>ResultSet</code> row into an <code>Object[]</code>.
     * This implementation copies column values into the array in the same
     * order they're returned from the <code>ResultSet</code>. Array elements
     * will be set to <code>null</code> if the column was SQL NULL.
     *
     * @param rs ResultSet that supplies the array data
     * @return the newly created array
     * @throws SQLException if a database access error occurs
     */
    @Override
    public Object[] createObjectFromResultSet(ResultSet rs) throws SQLException {
        return ROW_PROCESSOR.toArray(rs);
    }
}
